<template>
  <el-dialog
    :visible.sync="dialog"
    :close-on-click-modal="false"
    :title="title"
    width="800px"
    :append-to-body="true"
    top="5px"
    @close="doClose"
  >
    <el-container>
      <div>
        <el-form ref="editForm" :model="entity" :rules="rules" label-width="auto" size="mini">
          <el-row>
            <el-col :span="12">
              <el-form-item size="mini" label="档期:" prop="scheduleId">
                <el-input v-model="scheduleSubject" size="mini" placeholder="请输入" disabled style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="采购ID:" :required="!disabled" prop="buyerId">
                <el-select v-model="entity.buyerId" placeholder="请选择" size="mini" :disabled="disabled" style="width: 250px;" clearable>
                  <el-option v-for="item in parent.buyerIds" :key="item" :label="item" :value="item" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item size="mini" label="部门:" :required="!disabled" prop="deptNbr">
                <el-input v-model="entity.deptNbr" size="mini" maxlength="2" show-word-limit placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="品类:" :required="!disabled" prop="cateId">
                <el-input v-model="entity.cateId" size="mini" maxlength="2" show-word-limit placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item size="mini" label="生效日期:" :required="!disabled" prop="promotionStartDate">
                <el-date-picker v-model="entity.promotionStartDate" size="mini" type="date" format="MM/dd/yyyy" placeholder="选择日期" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="结束日期:" :required="!disabled" prop="promotionEndDate">
                <el-date-picker v-model="entity.promotionEndDate" size="mini" type="date" format="MM/dd/yyyy" placeholder="选择日期" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item size="mini" label="线上条码:" :required="!disabled" prop="onlineUpcNbr">
                <el-input v-model="entity.onlineUpcNbr" maxlength="13" show-word-limit size="mini" placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="线上商品名称:" prop="onlineItemName">
                <el-input v-model="entity.onlineItemName" maxlength="128" show-word-limit size="mini" placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item size="mini" label="价格/满减力度:" prop="promotionPrice">
                <el-input v-model="entity.promotionPrice" size="mini" maxlength="128" show-word-limit placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="限购数量:" prop="buyLimited">
                <el-input v-model="entity.buyLimited" size="mini" maxlength="20" show-word-limit placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item size="mini" label="发送门店:" :required="!disabled" prop="sendStore">
                <el-input v-model="entity.sendStore" type="textarea" maxlength="2000" show-word-limit rows="2" size="mini" placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="不发送门店:" prop="omitSendStore">
                <el-input v-model="entity.omitSendStore" type="textarea" maxlength="2000" show-word-limit rows="2" size="mini" placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col v-if="parent.showOptionType" :span="12">
              <el-form-item size="mini" label="操作类型:" prop="optionType">
                <el-select v-model="entity.optionType" placeholder="请选择" size="mini" style="width: 250px;" :disabled="disabled" clearable>
                  <el-option v-for="item in getSelector().optionTypes" :key="item.value" :label="item.name" :value="item.value" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item size="mini" label="备注:" prop="remark">
                <el-input v-model="entity.remark" type="textarea" maxlength="1000" show-word-limit rows="2" size="mini" placeholder="请输入" :disabled="disabled" style="width: 250px;" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
    </el-container>
    <el-row v-if="showSaveButton" :gutter="20" style="margin-top:30px;">
      <el-col :span="12"><div style="text-align: right;"><el-button type="primary" round size="small" @click="doSave()">保存</el-button></div></el-col>
      <el-col :span="1"><div><el-button type="primary" size="small" round @click="doClose()">取消</el-button></div></el-col>
    </el-row>
  </el-dialog>
</template>
<script>
import { Message } from 'element-ui'
import { parseToString } from '@/utils/datefromat'
import { getSelector, emptyEntity } from './js/edit'
import { showNameByValue } from '@/utils/selectUtil'
import { add, update } from '@/api/olm/item-list'
import Rule from './js/rules'
import Method from './js/methods'
import Validate from './js/validates'

export default {
  props: {
    parent: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      dialog: false,
      loading: false,
      currentObj: this,
      activeName: 'edit',
      rule: new Rule(this),
      method: new Method(this),
      validate: new Validate(this),
      title: '',
      rules: {},
      entity: this.emptyEntity(),
      scheduleSubject: '',
      schedule: null
    }
  },
  computed: {
    showSaveButton: function() {
      return !this.entity.dataStatus ||
      this.entity.dataStatus === -1 ||
      this.entity.dataStatus === 0 ||
      this.entity.dataStatus === 1 ||
      this.entity.dataStatus === 4
    },
    disabled: function() {
      return this.entity.dataStatus !== undefined &&
      this.entity.dataStatus !== null &&
      this.entity.dataStatus !== -1 &&
      this.entity.dataStatus !== 0 &&
      this.entity.dataStatus !== 1 &&
      this.entity.dataStatus !== 4
    }
  },
  created() {
    this.$nextTick(() => {
    })
  },
  mounted() {
    this.$nextTick(() => {
      this.rule.initRules()
    })
  },
  methods: {
    getSelector,
    emptyEntity,
    showNameByValue,
    parseToString,
    // 关闭
    doClose() {
      this.dialog = false
      this.entity = this.emptyEntity()
      this.selectedCategorys = []
      this.$refs.editForm.clearValidate()
      // this.parent.toQuery()
    },
    // 保存
    doSave() {
      var _this = this
      // console.log('entity:' + JSON.stringify(this.entity))
      this.$refs.editForm.validate(valid => {
        if (valid) {
          this.$confirm('确认保存？', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.loading = true
            if (!this.entity.id) { // 新增
              add(this.entity).then(res => {
                Message({
                  message: '保存成功',
                  type: 'success',
                  duration: 2 * 1000
                })
                _this.doClose()
                _this.parent.toQuery()
                this.loading = false
              }, error => {
                console.log(error)
                this.loading = false
              })
            } else { // 修改
              update(this.entity).then(res => {
                Message({
                  message: '修改成功',
                  type: 'success',
                  duration: 2 * 1000
                })
                _this.doClose()
                _this.parent.toQuery()
                this.loading = false
              }, (error) => {
                console.log(error)
                this.loading = false
              })
            }
          })
        } else {
          Message({
            message: '校验失败',
            type: 'warning',
            duration: 2 * 1000
          })
        }
      })
    }
  }
}
</script>
<style>
</style>
